import { createRouter, createWebHistory } from 'vue-router'
import { ElMessage } from 'element-plus'
import MainLayout from '../layout/MainLayout.vue'
import LoginView from '../views/LoginView.vue'
import StudentHome from '../views/Home/StudentHome.vue'
import StaffHome from '../views/Home/StaffHome.vue'
import QueryView from '../views/QueryView.vue'
import LeaveView from '../views/Leave/LeaveView.vue'
import LeaveApprovalView from '@/views/Leave/LeaveApprovalView.vue'
import ScholarshipView from '../views/Scholarship/ScholarshipView.vue'
import ScholarshipStaffView from '@/views/Scholarship/ScholarshipStaffView.vue'
import CourseView from '../views/CourseView.vue'
import SettingsView from '../views/SettingsView.vue'
// 已统一到根视图目录，使用同一个审批页面组件
// 留意：此组件同时用于 leave-staff 与 leave-approval 两条路由
// import LeaveApprovalView from '@/views/Leave/LeaveApprovalView.vue'
import StudentDataView from '@/views/StudentData.vue'
import StudentIDView from '@/views/ManagerPages/StudentID.vue'
import StudentDetailView from '@/views/ManagerPages/StudentDetail.vue'
import TeacherIDView from '@/views/ManagerPages/TeacherID.vue'
import ComprePageView from '@/views/ManagerPages/ComprePage.vue'
import StudentPersonalInfoView from '@/views/PersonalInfo/StudentPersonalInfo.vue'
import StaffPersonalInfoView from '@/views/PersonalInfo/StaffPersonalInfo.vue'

function getRoleId(): number {
  let localUser: any = {}
  try {
    const raw = localStorage.getItem('userInfo')
    localUser = raw ? JSON.parse(raw) : {}
  } catch (e) {
    localUser = {}
  }
  return Number(localUser?.roleId) || 0
}

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      redirect: '/login'
    },
    {
      path: '/login',
      name: 'login',
      component: LoginView,
      meta: { title: '登录' }
    },
    {
      path: '/main',
      component: MainLayout,
      redirect: '/main/home',
      children: [
        {
          path: 'home',
          name: 'home',
          redirect: () => (getRoleId() === 4 ? '/main/home-student' : '/main/home-staff'),
          meta: { title: '首页' }
        },
        {
          path: 'home-student',
          name: 'home-student',
          component: StudentHome,
          meta: { title: '首页-学生', roles: [4] }
        },
        {
          path: 'home-staff',
          name: 'home-staff',
          component: StaffHome,
          meta: { title: '首页-教职工', roles: [1, 2, 3] }
        },
        {
          path: 'query',
          name: 'query',
          component: QueryView,
          meta: { title: '信息查询', roles: [1, 2, 3, 4] }
        },
        {
          path: 'leave',
          name: 'leave',
          redirect: () => (getRoleId() === 4 ? '/main/leave-student' : '/main/leave-staff'),
          meta: { title: '请假' }
        },
        {
          path: 'leave-student',
          name: 'leave-student',
          component: LeaveView,
          meta: { title: '请假申请（学生）', roles: [4] }
        },
        {
          path: 'leave-staff',
          name: 'leave-staff',
          component: LeaveApprovalView,
          meta: { title: '请假（教职工）', roles: [1, 2, 3] }
        },
        {
          path: 'scholarship',
          name: 'scholarship',
          component: ScholarshipView,
          meta: { title: '奖学金', roles: [4] }
        },
        {
          path: 'course',
          name: 'course',
          component: CourseView,
          meta: { title: '选课', roles: [4] }
        },
        {
          path: 'leave-approval',
          name: 'leave-approval',
          component: LeaveApprovalView,
          meta: { title: '请假审批', roles: [1, 2, 3] }
        },
        {
          path: 'scholarship-approval',
          name: 'scholarship-approval',
          component: ScholarshipStaffView,
          meta: { title: '奖学金审批', roles: [1, 2] }
        },
        {
          path: 'student-info',
          name: 'student-info',
          component: StudentDataView,
          meta: { title: '学生信息', roles: [1, 2, 3] }
        },
        {
          path: 'personal',
          name: 'personal',
          redirect: () => (getRoleId() === 4 ? '/main/personal-student' : '/main/personal-staff'),
          meta: { title: '个人信息' }
        },
        {
          path: 'personal-student',
          name: 'personal-student',
          component: StudentPersonalInfoView,
          meta: { title: '个人信息-学生', roles: [4] }
        },
        {
          path: 'personal-staff',
          name: 'personal-staff',
          component: StaffPersonalInfoView,
          meta: { title: '个人信息-教职工', roles: [1, 2, 3] }
        },
        {
          path: 'settings',
          name: 'settings',
          component: SettingsView,
          meta: { title: '设置', roles: [1] }
        },
        {
          path: 'student-id',
          name: 'student-id',
          component: StudentIDView,
          meta: { title: '学生账号管理', roles: [1] }
        },
        {
          path: 'student-detail/:id',
          name: 'student-detail',
          component: StudentDetailView,
          meta: { title: '学生详情', roles: [1] }
        },
        {
          path: 'teacher-id',
          name: 'teacher-id',
          component: TeacherIDView,
          meta: { title: '教师账号管理', roles: [1] }
        },
        {
          path: 'compre',
          name: 'compre',
          component: ComprePageView,
          meta: { title: '综合管理', roles: [1] }
        }
      ]
    }
  ]
})

router.beforeEach((to, _from, next) => {
  const roles = (to.meta as any)?.roles as number[] | undefined
  if (!roles || roles.length === 0) {
    next()
    return
  }
  const roleId = getRoleId()
  if (roles.includes(roleId)) {
    next()
  } else {
    ElMessage && ElMessage.warning('无权限访问该页面')
    next('/main/home')
  }
})

export default router
